//
//  YWLittleVideoPlayerCell2.m
//  YueshengToutiao
//
//  Created by yellow on 2021/4/22.
//  Copyright © 2021 YW. All rights reserved.
//

#import "YWLittleVideoPlayerCell2.h"

@interface YWLittleVideoPlayerCell2 ()

@property (weak, nonatomic) IBOutlet UIView *functionView;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UIButton *authorBtn;
@property (weak, nonatomic) IBOutlet UIButton *likeBtn;
@property (weak, nonatomic) IBOutlet UIButton *shareBtn;
- (IBAction)authorBtnClick:(UIButton *)sender;
- (IBAction)likeBtnClick:(UIButton *)sender;
- (IBAction)shareBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UIButton *playOrPauseBtn;
- (IBAction)playOrPauseBtnClick:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UIView *videoBgView;
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UIButton *loadingView;
@property (weak, nonatomic) IBOutlet UIView *videoView;

@property (weak, nonatomic) IBOutlet UIView *grayView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomViewBottomConstraint;
@property (weak, nonatomic) IBOutlet YWConcernBtn *concernBtn;
- (IBAction)concernBtnClick:(UIButton *)sender;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *authorBtnBottomConstraint;

@end
@implementation YWLittleVideoPlayerCell2


- (IBAction)concernBtnClick:(UIButton *)sender {
   
}


+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"YWLittleVideoPlayerCell2";
    YWLittleVideoPlayerCell2 *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        // 从xib中加载cell
        cell = [[[NSBundle mainBundle] loadNibNamed:@"YWLittleVideoPlayerCell2" owner:nil options:nil] lastObject];
    }
    return cell;
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    if (isIphoneX) {
        self.authorBtnBottomConstraint.constant = 15+25;
    }
    else{
        
        self.authorBtnBottomConstraint.constant = 15;
    }
    
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
       gradientLayer.colors = @[(__bridge id)YWColorRGBA(0, 0, 0, 0.8).CGColor, (__bridge id)[UIColor clearColor].CGColor];
//       gradientLayer.locations = @[@0, @1.0];
       gradientLayer.startPoint = CGPointMake(0, 1.0);
       gradientLayer.endPoint = CGPointMake(0, 0);
       gradientLayer.frame = CGRectMake(0, 0, Wi, self.grayView.height);
       [self.grayView.layer addSublayer:gradientLayer];
    
    
    
    self.authorBtn.layer.cornerRadius = 20;
    self.authorBtn.clipsToBounds = YES;
    
    //单击手势暂停播放
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];

    [self addGestureRecognizer:tap];
    
    //loadingView的旋转动画一直存在就行、不用考虑显示隐藏、所以写在了awakeFromNib
    //添加loading动画
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    rotationAnimation.duration = 0.9;
    rotationAnimation.cumulative = YES;
    rotationAnimation.repeatCount = MAXFLOAT;
    rotationAnimation.removedOnCompletion = NO;//这个属性才会重新出现界面时不会停止动画
    [self.loadingView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    
    //简单点、不要loadingView了
    self.loadingView.hidden = YES;
    
    //改为VC来监听通知
    //监听完成点赞或者取消点赞通知
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(praiseOrCancernPraise:) name:praiseOrCancelPraiseNotification object:nil];
    
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(addOrCancelConcern:) name:addOrCancelConcernNotification object:nil];
}

//刚出来的时候
-(void)setStatus:(YWGetNewsDataStatus *)status{
    _status = status;
    
//    YWGetNewsDataStatus *status = playerStatus.status;
    
    //播放按钮还是隐藏它
    self.playOrPauseBtn.hidden = YES;
    
    ///------------------------
    
    //iconView是一直显示、刚出来就会见到
    if ([status.Img isKindOfClass:[NSNull class]] ||  status.Img == nil) {
        status.Img = @"";
    }
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:status.Img] placeholderImage:[UIImage imageWithColor:[UIColor blackColor]]];
    
    if ([status.AuthorImg isKindOfClass:[NSNull class]] ||  status.AuthorImg == nil) {
        status.AuthorImg = @"";
    }
    
    [self.authorBtn sd_setImageWithURL:[NSURL URLWithString: status.AuthorImg] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"头像3"]];
    
    
    self.nameLabel.text = status.Author;
    
    
    
    NSString *detailString = status.Describe;
    
    if ([detailString isKindOfClass:[NSNull class]]||detailString == nil) {
        detailString = @"";
    }

    NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:6];

        NSMutableAttributedString * attributedStringTitle = [[NSMutableAttributedString alloc] initWithString:detailString];
           [attributedStringTitle addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [detailString length])];
       [self.detailLabel setAttributedText:attributedStringTitle];
    
//    self.likeBtn.selected = status.IsLike.boolValue;
    
    
    if (status.IsLike.integerValue == 1) {
        
//        [self.praiseBtn setImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
//
//        [self.praiseBtn setTitleColor:mainColor forState:UIControlStateNormal];
        
        
        
        self.likeBtn.selected = YES;
        
        [self.likeBtn setTitle:[NSString stringWithFormat:@"%@",status.Praise] forState:UIControlStateNormal];
    }
    else{
        
//         [self.praiseBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
//
//        [self.praiseBtn setImage:[UIImage imageNamed:@"赞-未选中"] forState:UIControlStateNormal];
        
        self.likeBtn.selected = NO;
        
        
        [self.likeBtn setTitle:[NSString stringWithFormat:@"点赞"] forState:UIControlStateNormal];
    }
    
    
    //老师的是否关注还没有
    if (status.IsFocus.intValue == 1) {
        self.concernBtn.isSelect = YES;
    }
    else{
        self.concernBtn.isSelect = NO;
    }
    
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)tapAction:(UITapGestureRecognizer *)sender {
 
    
}
- (IBAction)playOrPauseBtnClick:(UIButton *)sender {
    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    
}
#pragma mark - 一些functionView方法
- (IBAction)shareBtnClick:(UIButton *)sender {
  
    
}


- (IBAction)likeBtnClick:(UIButton *)sender {
    
    
}


-(void)praiseOrCancernPraise:(NSNotification *)notification{
    
    //拿出模型
    YWGetNewsDataStatus *dataStatus = notification.userInfo[@"context"];
    
    if ([self.status.NewsId isEqualToString:dataStatus.NewsId]) {
        
        
        if (dataStatus.IsLike.integerValue == 1) {
            
            self.likeBtn.selected = YES;
            
            [self.likeBtn setTitle:[NSString stringWithFormat:@"%@",dataStatus.Praise] forState:UIControlStateNormal];
        }
        else{
            
            self.likeBtn.selected = NO;
            
            
            [self.likeBtn setTitle:[NSString stringWithFormat:@"点赞"] forState:UIControlStateNormal];
        }
        
        
        
        
//        self.likeBtn.selected = dataStatus.IsLike.boolValue;
    }
   
}


//-(void)addOrCancelConcern:(NSNotification *)notification{
//
//    //拿出模型
//    NSString *ID = notification.userInfo[@"ID"]; //基金名称
//    NSNumber *isConcern = notification.userInfo[@"isConcern"]; //是否关注
//
//    if ([self.status.UserId isEqualToString:ID]) {
//        self.concernBtn.isSelect = isConcern.boolValue;
//    }
//
//
//
//
//}


- (IBAction)authorBtnClick:(UIButton *)sender {
    
}

@end
